package com.lxhll.personnel.controller;

import com.lxhll.personnel.common.JwtUtil;
import com.lxhll.personnel.core.ResponseData;
import com.lxhll.personnel.entity.Employee;
import com.lxhll.personnel.service.EmployeeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/login")
    public ResponseData login(@RequestBody Employee loginE){

        Employee employee=employeeService.getEmployeeById(loginE.getId());
        if(employee==null){
            return ResponseData.error("无此员工");
        }
        //对数据库加密的密码与前端传来的密码也加密,进行对比，
        String password="";

        password= DigestUtils.md5DigestAsHex(loginE.getPassword().getBytes());

        if(StringUtils.isBlank(password)){
            return ResponseData.error("无登录权限，请联系人事部");
        }
        if(!employee.getPassword().equals(password)){
            return ResponseData.error("密码错误");
        }
        String token=JwtUtil.sign(employee);
        return ResponseData.ok(token);
    }
}
